cc.Class({
  extends: cc.Component,

  properties: {
    button_sound: {
      //爆炸音效
      type: cc.AudioClip,
      default: null,
    },
    hp:3,
  },
  onLoad() {
    this.init();
    this.isCreateBlood = false;
    if (DataMangrt.level) {
      DataMangrt.enemyNum = 0;
      DataMangrt.nowKillEnemyNum = 0;
      let guanqia = "level" + DataMangrt.level; //获取当前关卡
      for (let key in DataMangrt.killEnemyNum) {
        if (key == guanqia) {
          //遍历killEnemyNum，获取当前关卡的胜利条件（击杀敌人数量）
          DataMangrt.enemyNum = DataMangrt.killEnemyNum[key];
          console.log("获取当前关卡需要击杀敌人数量", DataMangrt.enemyNum);
        }
      }
    }
  },
  init: function () {
    this.node.active = true;
    var anim = this.getComponent(cc.Animation);
    anim.play("playernomal");
  },
  onCollisionEnter: function (other, self) {
    //tag=10玩家战机
    if (game.isStart == false) return;
    if (other.tag == 31) {//血包
      other.node.destroy();
      let hp = this.hp++;
      game.playerAX[hp].active = true;
    } else {
      //    this.onclickSound();
      this.schedule(()=>{//受伤效果
        let boolen = game.hitWarning.active;
        game.hitWarning.active = !boolen;
      },0.3,5 );
      let hp = --this.hp;
      game.playerAX[hp].active = false;
      if (this.hp <= 0) {
        this.isOver();
      }
    }
    console.log("现在血量是", this.hp);
  },

  isOver: function () {
    game.isStart = false;
    var anim = this.getComponent(cc.Animation);
    anim.play("playerDisappeared");
    anim.disappeared = function () {
      this.node.active = false;
      this.scheduleOnce(() => {
        game.isOverBg.active = true;
      }, 0.2);
    };
  },

  onclickSound: function (event, data) {
    //点击音效
    cc.audioEngine.play(this.button_sound, false, 1);
  },
 
  update(dt) {
    let e = this;
    if (e.isCreateBlood==false) {//检测是否生成血包
    
        if (e.hp < 2) {
            e.isCreateBlood = true;
            var randomNum = Math.floor(Math.random() * 5) + 1;
            e.scheduleOnce(() => {
                game.creatProps();//生成血包
            }, randomNum);
        }
    }
  },
});
